package main

import (
	"GORM_Demo04/part08/MyPojo"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
)

/*	常用方法

	【8】Create：添加数据
	【9】Save：新增或更新,根据结构体传入的实例中是否含有主键确定
			--有主键更新
			--没有新增加

*/

func main() {
	db, err := gorm.Open("mysql", "root:123456@tcp(192.168.11.189:3306)/lztest?charset=utf8&parseTime=True&loc=Local")

	if err != nil {
		panic(err)
	}
	defer db.Close()

	db.LogMode(true)

	/*	【8】Create：添加数据
		【9】Save：添加数据*/

	//【8】Create：添加数据只能添加一条
	/*	user := MyPojo.User{
			UName: "王强",
			UAge:  48,
		}
		db.Create(&user)*/

	//【9】Save：添加数据只能添加一条

	//结构体实例中不含主键
	//INSERT INTO `users` (`u_name`,`u_age`) VALUES ('开开',48)
	user := MyPojo.User{

		UName: "开开",
		UAge:  48,
	}

	db.Save(&user)

	//结构体实例中含有主键
	//UPDATE `users` SET `u_name` = '开开', `u_age` = 48  WHERE `users`.`uid` = 4
	/*user := MyPojo.User{
		UID:   4,
		UName: "开开",
		UAge:  48,
	}

	db.Save(&user)
	*/
}
